﻿using FirstFloor.ModernUI.Presentation;
using FirstFloor.ModernUI.Windows.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Input;
using appCtx = EasyInvoice.ApplicationContext.AppContext;
using Microsoft.Practices.Unity;

namespace EasyInvoice.WPF.ViewModel
{
    class SettingsDatabaseViewModel : NotifyPropertyChanged
    {
        private string databaseFileLocation  = EasyInvoice.WPF.Properties.Settings.Default.DatabaseFileLocation;

        private string databasePassword = EasyInvoice.WPF.Properties.Settings.Default.DatabasePassword;

        public string DatabaseFileLocation
        {
            get { 
                return this.databaseFileLocation;
            }
            set {
                if (this.databaseFileLocation != value)
                { this.databaseFileLocation = value;
                OnPropertyChanged("DatabaseFileLocation");

                }

            }

        }

        public string DatabasePassword
        {
            get
            {
                return this.databasePassword;
            }
            set
            {
                if (this.databasePassword != value)
                {
                    this.databasePassword = value;
                    OnPropertyChanged("DatabasePassword");

                }

            }

        }

        private Services.DatabaseService _serviceDatabase; 

        public SettingsDatabaseViewModel()
        {
            _serviceDatabase = appCtx.Instance.IoC.Resolve<Services.DatabaseService>();
        }

        private ICommand showBrowseCommand;

        public ICommand ShowBrowseCommand
        {
            get
            {
                if (showBrowseCommand == null)
                {
                    showBrowseCommand = new Common.CommandBase(
                        i => this.BrowseFiles(),
                        null);
                }
                return showBrowseCommand;
            }
        }

        private ICommand showSaveCommand;

        public ICommand ShowSaveCommand
        {
            get
            {
                if (showSaveCommand == null)
                {
                    showSaveCommand = new Common.CommandBase(
                        i => this.SaveSettings(),
                        i => (Properties.Settings.Default.DatabaseFileLocation != this.databaseFileLocation) || (Properties.Settings.Default.DatabasePassword != this.databasePassword));
                }
                return showSaveCommand;
            }
        }

        private ICommand showConnectCommand;

        public ICommand ShowConnectCommand
        {
            get
            {
                if (showConnectCommand == null)
                {
                    showConnectCommand = new Common.CommandBase(
                        i => this.ConnectDatabase(),
                        i=> SqlCEDAL.Properties.Settings.Default.ConnectionString.ToString() != string.Format("Data Source={0}; Password={1};",
                                                        this.databaseFileLocation,
                                                        this.databasePassword));
                }
                return showConnectCommand;
            }
        }

        private ICommand showCreateCommand;

        public ICommand ShowCreateCommand
        {
            get
            {
                if (showCreateCommand == null)
                {
                    showCreateCommand = new Common.CommandBase(
                        i => this.CreateDatabase(),
                        i=> !System.IO.File.Exists(this.databaseFileLocation));
                }
                return showCreateCommand;
            }
        }

        private void CreateDatabase()
        {
            SqlCEDAL.Properties.Settings.Default["ConnectionString"] = string.Format("Data Source=\"{0}\"; Password='{1}';",
                   this.databaseFileLocation,
                   this.databasePassword);
            _serviceDatabase.Create();
        }

        private void ConnectDatabase()
        {
            SqlCEDAL.Properties.Settings.Default["ConnectionString"] = string.Format("Data Source=\"{0}\"; Password='{1}';",
                    this.databaseFileLocation,
                    this.databasePassword);
            try
            {
                //ddo some connection shit
                Repository.Reset();
            }
            catch (Exception ex)
            {
                ModernDialog.ShowMessage(ex.Message, "Failed Connection", System.Windows.MessageBoxButton.OK);
            }
         }

        private void SaveSettings()
        {
            EasyInvoice.WPF.Properties.Settings.Default.DatabaseFileLocation = this.databaseFileLocation;
            EasyInvoice.WPF.Properties.Settings.Default.DatabasePassword = this.databasePassword;
            Properties.Settings.Default.Save();
        }

        private void BrowseFiles()
        {

            EasyInvoice.WPF.Common.BrowseFiles folderBrowser = new EasyInvoice.WPF.Common.BrowseFiles
            {
                Title = "Browse For Database",
                SelectedPath = EasyInvoice.WPF.Properties.Settings.Default.DatabaseFileLocation,
                Description = "select the database file:",
                filterExtension = ".sdf"
            };
           //folderBrowser.Buttons = new System.Windows.Controls.Button[] { folderBrowser.OkButton, folderBrowser.CancelButton };
            if (folderBrowser.ShowDialog() == true)
                DatabaseFileLocation = folderBrowser.SelectedPath;

            this.OnPropertyChanged("ExporFolderLocation");
        }
        
    }
}
